package com.gedmanager.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import com.gedcorp.gedmanager.MainActivity;

import android.os.Environment;

public class PropertiesHandler {
	static String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()
			+ "/" + MainActivity.PACKAGE_CONFIG_DIR + "/" + "config.properties";

	public static void init(){
		
		File file = new File(filePath);
		
		if (file.exists()) {
			System.out.println("arquivo ja existe");
			return;
		}
		System.out.println("arquivo NAO existe");
		Properties prop = new Properties();
		OutputStream output = null;

		try {

			output = new FileOutputStream(filePath);

			// set the properties value
			prop.setProperty("emailrelatorio", "seuemail1@exemplo.com");
			prop.setProperty("emailqrcodes", "seuemail2@exemplo.com");
			prop.setProperty("termos", "false");

			// save properties to project root folder
			prop.store(output, null);

		} catch (IOException io) {
			io.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}
	
	public static String getProperty(String propertyKey) {
		 
		String result = "";
		Properties prop = new Properties();
 
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(filePath);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			prop.load(inputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String propertyValue = prop.getProperty(propertyKey);

		System.out.println(result + "\nprop " + propertyKey + "  value=" + propertyValue);
		try {
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return propertyValue;
	}
	
	public static void setProperty(String propertyKey, String value) {
		FileInputStream in = null;
		try {
			in = new FileInputStream(filePath);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Properties props = new Properties();
		try {
			props.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		props.setProperty(propertyKey, value);
		try {
			props.store(out, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}